<%
/**
 *  edit 组织机构编辑
 * 
 * @author  李荣华
 * @version v1.0
 * @date    2017-11-22
 * @since   JSP 2.3.1
 */
%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="at" uri="http://www.jketing.com/at"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>机构编辑</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta content="no-cache" http-equiv="Pragma"/>
	<meta content="no-cache" http-equiv="Cache-Control"/>
	<meta content="0" http-equiv="Expires"/>
	<meta name="GENERATOR" content="MSHTML 8.00.6001.18854"/>
	<link rel="stylesheet" type="text/css" href="<at:url css="edit.css"/>" />
	<link rel="stylesheet" type="text/css" href="<at:url css="banner_button.css"/>" />
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/scripts/Common/validation-engine-2.2/css/validationEngine.jquery.css" />
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/scripts/Common/validation-engine-2.2/css/template.css" />
	<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/common.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/select.js"></script>
	<script type="text/javascript" src="<at:url js="edit.js"/>"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/Common/validation-engine-2.2/languages/jquery.validationEngine-cn.js" charset="utf-8"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/Common/validation-engine-2.2/jquery.validationEngine.js" charset="utf-8"></script>
	<script type="text/javascript">
	/**
	 * 重置操作
	 */
	function reset() {
		document.getElementById("webForm").reset();
	}	
	
	/**
	 * 返回列表
	 */
	function ret(){				
		if(window.confirm('数据没保存，你确定要返回列表吗？')){
			window.history.back(-1);//因要删除进入更新编辑页时保存在session中的查询数据，故不能使用js退回
		}
	}
	
	/**
	 * 提交数据
	 */
	function submit() {
		/* if($("#webForm").validationEngine('validate')){
			$("#webForm").submit();
		} */
		$("#unitNatureId").removeAttr("disabled");//清除灰化属性才能提交数据
		$("#subOrdinationId").removeAttr("disabled");
		$("#classifyToManageId").removeAttr("disabled");
		$("#webForm").submit();
	}

	/**
	 * 照片上传操作 
	 */
	function init(){
		var upload = $('#uploadPhoto'), interval;
		new AjaxUpload(upload, {
			action: 'adjunct!upImage', 
			name: 'file',
			onSubmit : function(file, ext){
				// change button text, when user selects file			
				upload.text('<s:text name="js.common.uploadPhoto"></s:text>');
				// If you want to allow uploading only 1 file at time,
				// you can disable upload button
				this.disable();
				
				// Uploding -> Uploading. -> Uploading...
				interval = window.setInterval(function(){
					var text = upload.text();
					if (text.length < 10){
						upload.text(text + '.');					
					} else {
						upload.text('<s:text name="js.common.uploadPhoto"></s:text>');				
					}
				}, 200);
			},
			onComplete: function(file, response){
				var json = eval('(' + response + ')');
				window.clearInterval(interval);
				document.getElementById("uploadPhoto").value = json.id;
				//$("#webForm").validate().element($("#photo"));
				document.getElementById("uploadPhoto").innerHTML = "<img width='200' height='127' src='adjunct!getImage?af.id=" + json.id + "' alt='上传照片' />";	
				// enable upload button
				this.enable();				
			}
		});
	}
	
	function typeChange(){
		var type = $("#entityType").val(); 
		if('branch'==type){
			var parentId = $("#parentId").val();
			var randomNumber = Math.random();
			//alert(parentId);
			$.ajax({
				   type: "GET",
				   url: "${pageContext.request.contextPath}/organ/getParent",
				   data: {id:parentId,rad:randomNumber},
				   async: false,
				   dataType:"json",
				   success: function (data, textStatus){
					    //$("#unitNatureId").val(data.unitNature.id); 
						//$("#subOrdinationId").val(data.subOrdination.id); 
						//$("#classifyToManageId").val(data.classifyToManage.id);
						$("#unitNatureId").find("option[value='" + data.unitNature.id + "']").attr("selected","selected");
						$("#subOrdinationId").find("option[value='" + data.subOrdination.id + "']").attr("selected","selected");
						$("#classifyToManageId").find("option[value='" + data.classifyToManage.id + "']").attr("selected","selected");
						
						$("#areaId").val(data.region.id);
						$("#areaName").val(data.region.name);
						$("#longitude").val(data.longitude);
						$("#latitude").val(data.latitude);
						$("#addr").val(data.addr);
						$("#unitNatureId").attr("disabled","disabled");
						$("#subOrdinationId").attr("disabled","disabled");
						$("#classifyToManageId").attr("disabled","disabled");
						$("#areaName").attr("onclick","alert('机构类型为部门时不能选择行政区域！');"); 
						$("#addr").attr("onclick","alert('机构类型为部门时不能选择位置！');"); 
						//$("#classifyToManageId").selectmenu('refresh'); 
						
					},
					error:function(req, textStatus, errorThrown){
						var errMsg = "";
						if(req.getResponseHeader("error_msg"))
							errMsg += "\n"+req.getResponseHeader("error_msg")+"\n\n";
						alert("对不起，请求服务器出现异常！ "+errMsg+"请与管理员联系或稍后再试！");
						$("#unitNatureId").val(""); 
						$("#subOrdinationId").val(""); 
						$("#classifyToManageId").val(""); 
						$("#unitNatureId").removeAttr("disabled");
						$("#subOrdinationId").removeAttr("disabled");
						$("#classifyToManageId").removeAttr("disabled");
						$("#areaName").attr("onclick","regionSingleSelect('areaId','areaName');"); 
						$("#addr").attr("onclick","positionSelect();"); 
					}
				});
		}
		else{
			$("#unitNatureId").val(""); 
			$("#subOrdinationId").val(""); 
			$("#classifyToManageId").val(""); 
			$("#unitNatureId").removeAttr("disabled");
			$("#subOrdinationId").removeAttr("disabled");
			$("#classifyToManageId").removeAttr("disabled");
			$("#areaName").attr("onclick","regionSingleSelect('areaId','areaName');"); 
			$("#addr").attr("onclick","positionSelect();"); 
		}
		
	}
		
	/**
	 * 加载照片
	 */
	/*$(document).ready(function(){
		init();
	});*/
	
	/**
	 * 打开区域单选对话框
	 */
	function regionSingleSelect(id,name){
		var myObject = new Object();
		myObject.id = document.getElementById(id);
		myObject.name = document.getElementById(name);	
	    myObject.pWindow=window;
	    window.showModalDialog('${pageContext.request.contextPath}/region/singleSelect',
	    myObject, 'dialogWidth: 252px;dialogHeight: 410px;edge: Raised; center: Yes; help: Yes; resizable: No; scroll:No; status: No;');				
	}
	
	/**
	 * 打开地图定位对话框
	 */
	function positionSelect(){
		var addr = $("#addr").val();
		var myObject = new Object();
		myObject.longitude = document.getElementById("longitude");
		myObject.latitude = document.getElementById("latitude");
		myObject.addr = document.getElementById("addr");
	    window.showModalDialog('${pageContext.request.contextPath}/common/positionSelect',
	    myObject, 'dialogWidth: 800px; dialogHeight: 600px; edge: Raised; center: Yes; help: Yes; resizable: No; scroll:No; status: No;');
	    
	}
	
	/**
	 * validation-engine 表单验证 
	 */
	jQuery(document).ready(function(){
          	jQuery("#webForm").validationEngine({   
         		validationEventTriggers:"keyup blur",  //触发的事件  validationEventTriggers:"keyup blur",   
          	inlineValidation: true,//是否即时验证，false为提交表单时验证,默认true   
          	success :  false,//为true时即使有不符合的也提交表单,false表示只有全部通过验证了才能提交表单,默认false   
          	promptPosition: "topRight"//提示所在的位置，topLeft, topRight, bottomLeft,  centerRight, bottomRight   
          	//failure : function() { alert("验证失败，请检查。");  }//验证失败时调用的函数   
          	//success : function() { callSuccessFunction() },//验证通过时调用的函数   
          	});
      	});
	
	</script>
	<c:set value="${pageContext.request.contextPath}/organ" var="url"></c:set>
	<c:if test="${entity.id != null }">
		<c:set value="${pageContext.request.contextPath}/organ/${entity.id}" var="url"></c:set>
	</c:if>
</head>
<body>		
	<form:form action="${url}" method="POST" enctype="multipart/form-data"
		modelAttribute="entity" class="form-style" id="webForm" name="webForm">
		<c:if test="${entity.id!=null}">
			<form:hidden path="id"/>
			<input type="hidden" name="_method" value="PUT"/>
		</c:if>
		<input type="hidden" name="page" value="${page}"/>	
		<input type="hidden" name="order" value="${order}" />
		<!-- 按钮行 -->
		<div class="edit_top_f">
			<table width="100%" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td width="4" height="24" id="kj2z03"></td>
					<td class="biao3_xia">
						<table width="100%" border="0" cellspacing="0" cellpadding="0">
							<tr>
								<td height="24" class="biao4_zhong">
									<div id="nav_01">
										<ul>
											<li><a href="#" onclick="ret();">返回</a></li>
											<li><a href="#" onclick="submit();">提交</a></li>
										</ul>
									</div>
								</td>
							</tr>
						</table>
					</td>
					<td width="4" height="24" id="kj2y03"></td>
				</tr>
			</table>
		</div>
		<div class="edit_main_f">
			<table width="100%" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td>
						<table width="100%" border="0" cellspacing="0" cellpadding="0"
							rules="groups">
							<tr>
								<td width="20%" height="100%" class="biantiao_you">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
										<tr>
											<td align="right" valign="middle" class="font_1">
												<nobr>上级机构</nobr>
											</td>																				
										</tr>
									</table>
								</td>
								<td width="30%" height="100%" class="biantiao_you">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
										<tr>																				
											<td align="left">
												<form:select path="parent.id" items="${list}" 
													class="validate[required]" id="parentId"
													style="width:90.5%" itemLabel="name" itemValue="id">
												</form:select>
											</td>
																															
										</tr>
									</table>
								</td>
								<td width="50%" height="100%" class="biantiao_you" rowspan="9" colspan="2">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
										<tr>
											<td width="100%" align="center" valign="middle" class="font_1" >
												<c:choose>
													<c:when test="${entity.id!=null}">
														<img src="${pageContext.request.contextPath}/organ/getImage/${entity.id}"  width="320px" height="160px" alt="机构照片" />
												   	</c:when> 
												   	<c:otherwise>
												   		<img src="${pageContext.request.contextPath}/res/image/organ_default.jpg" width="320px" height="160px" alt="机构照片" />
												   	</c:otherwise>  
												</c:choose>
											</td>																				
										</tr>
									</table>
								</td>													
							</tr>
																											
							<!-- 行分割线 -->
							<tr>
								<td height="1" class="biaotiao_xia" colspan="2"></td>																	
							</tr>																
							<tr>																
								 <td width="20%" height="100%" class="biantiao_you">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
										<tr>
											<td width="25%" align="right" valign="middle" class="font_1">
												<nobr>机构名称</nobr>
											</td>																				
										</tr>
									</table>
								</td>
								<td width="30%" height="100%" class="biantiao_you">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
										<tr>																				
											<td width="91%" align="left">
												<form:input path="name" class="validate[required]" id="objectName" style="width:90%"/>
												<font color="red">*</font>												
											</td>																																							
										</tr>
									</table>
								</td>																									
							</tr>
							<!-- 行分割线 -->
							<tr>
								<td height="1" class="biaotiao_xia" colspan="2"></td>																	
							</tr>																
							<tr>																
								 <td width="20%" height="100%" class="biantiao_you">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
										<tr>
											<td width="25%" align="right" valign="middle" class="font_1">
												<nobr>机构类型</nobr>
											</td>																				
										</tr>
									</table>
								</td>
								<td width="30%" height="100%" class="biantiao_you">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
										<tr>																				
											<td width="91%" align="left">
												<select class="validate[required]" id="entityType" name="type" onchange="typeChange();" style="width:90.5%">
													<option value="" >请选择</option>
													<c:choose>
														<c:when test="${entity.type=='branch'}">
															<option value="branch" selected='selected'>部门</option>
													   	</c:when> 
													   	<c:otherwise>
													   		<option value="branch" >部门</option>
													   	</c:otherwise>  
													</c:choose>
													<c:choose>
														<c:when test="${entity.type=='unit'}">
															<option value="unit" selected='selected'>单位</option>
													   	</c:when> 
													   	<c:otherwise>
													   		<option value="unit" >单位</option>
													   	</c:otherwise>  
													</c:choose>
													<c:choose>
														<c:when test="${entity.type=='group'}">
															<option value="group" selected='selected'>集团</option>
													   	</c:when> 
													   	<c:otherwise>
													   		<option value="group" >集团</option>
													   	</c:otherwise>  
													</c:choose>
												</select>
												<font color="red">*</font>															
											</td>																																							
										</tr>
									</table>
								</td>																									
							</tr>
							<!-- 行分割线 -->
							<tr>
								<td height="1" class="biaotiao_xia" colspan="2"></td>																	
							</tr>																
							<tr>																
								 <td width="20%" height="100%" class="biantiao_you">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
										<tr>
											<td width="25%" align="right" valign="middle" class="font_1">
												<nobr>单位性质</nobr>
											</td>																				
										</tr>
									</table>
								</td>
								<td width="30%" height="100%" class="biantiao_you">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
										<tr>																				
											<td width="91%" align="left">
												<select class="select" id="unitNatureId" name="unitNature.id" style="width:90.5%">
													<option value="">请选择</option>
													<c:forEach items="${unitNature}" var="item">
														<c:choose>
															<c:when test="${entity.unitNature.id==item.id}">
														   		<option value="${item.id}" selected='selected'>${item.name}</option>
														   	</c:when> 
														   	<c:otherwise>
														   		<option value="${item.id}">${item.name}</option>
														   	</c:otherwise>  
														</c:choose>
													</c:forEach>
												</select>
												<font color="red">*</font>												
											</td>																																							
										</tr>
									</table>
								</td>																									
							</tr>
							<!-- 行分割线 -->
							<tr>
								<td height="1" class="biaotiao_xia" colspan="2"></td>																	
							</tr>	
							<tr>
								<td width="20%" height="40" class="biantiao_you">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
											<tr>
												<td width="25%" align="right" class="font_1">
													<nobr>隶属关系</nobr>
												</td>																				
											</tr>
										</table>																		
								</td>
								<td width="30%" height="40" class="biantiao_you">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
										<tr>																				
											<td width="100%" align="left">
												<select class="select" id="subOrdinationId" name="subOrdination.id" style="width:90.5%">
													<option value="">请选择</option>
													<c:forEach items="${subOrdination}" var="item">
														<c:choose>
															<c:when test="${entity.subOrdination.id==item.id}">
														   		<option value="${item.id}" selected='selected'>${item.name}</option>
														   	</c:when> 
														   	<c:otherwise>
														   		<option value="${item.id}">${item.name}</option>
														   	</c:otherwise>  
														</c:choose>
													</c:forEach>
												</select>
												<font color="red">*</font>
											</td>																				
										</tr>
									</table>
								</td>
							</tr>	
							<!-- 行分割线 -->
							<tr>
								<td height="1" class="biaotiao_xia" colspan="4"></td>																	
							</tr>	
							<tr>
								<td width="20%" height="40" class="biantiao_you">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
											<tr>
												<td width="25%" align="right" class="font_1">
													<nobr>分类管理</nobr>
												</td>																				
											</tr>
										</table>																		
								</td>
								<td width="30%" height="40" class="biantiao_you">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
										<tr>																				
											<td width="100%" align="left">
												<select class="select"  id="classifyToManageId" name="classifyToManage.id" style="width:90.5%">
													<option value="">请选择</option>
													<c:forEach items="${classifyToManage}" var="item">
														<c:choose>
															<c:when test="${entity.classifyToManage.id==item.id}">
														   		<option value="${item.id}" selected='selected'>${item.name}</option>
														   	</c:when> 
														   	<c:otherwise>
														   		<option value="${item.id}">${item.name}</option>
														   	</c:otherwise>  
														</c:choose>
													</c:forEach>
												</select>
												<font color="red">*</font>
											</td>																				
										</tr>
									</table>
								</td>
								<td width="20%" height="40" class="biantiao_you">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
											<tr>
												<td width="25%" align="right" class="font_1">
													<nobr>机构照片</nobr>
												</td>																				
											</tr>
										</table>																		
								</td>
								<td width="30%" height="40">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
										<tr>																				
											<td width="100%" align="left">
												<input type="file" name="file" size="90%" style="width:90%"/>
											</td>																				
										</tr>
									</table>
								</td>
							</tr>	
							<!-- 行分割线 -->
							<tr>
								<td height="1" class="biaotiao_xia" colspan="4"></td>																	
							</tr>	
							<tr>
								<td width="20%" height="40" class="biantiao_you">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
										<tr>
											<td width="25%" align="right" class="font_1">
												<nobr>行政区域</nobr>
											</td>																				
										</tr>
									</table>																		
								</td>
								<td width="30%" height="40" class="biantiao_you">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
										<tr>																				
											<td width="100%" align="left">
												<input class="validate[required]"
														type="hidden" id="areaId" name="region.id"
														value="${entity.region.id}" /> 
													<input id="areaName"
														name="region.name"
														onclick="regionSingleSelect('areaId','areaName');"
														value="${entity.region.name}" alt="行政区域"
														readonly="readonly" style="width:90%" /> 
														<font color="red">*</font>
											</td>																				
										</tr>
									</table>
								</td>
								<td width="20%" height="40" class="biantiao_you">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
											<tr>
												<td width="25%" align="right" class="font_1">
													<nobr>前台展示</nobr>
												</td>																				
											</tr>
										</table>																		
								</td>
								<td width="30%" height="40">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
										<tr>																				
											<td width="100%" align="left">
												<form:input path="frontShow" 
															class="validate[custom[phone]]"
															id="tel"
															style="width:90%" />
											</td>																				
										</tr>
									</table>
								</td>
							</tr>	
							<!-- 行分割线 -->
							<tr>
								<td height="1" class="biaotiao_xia" colspan="4"></td>																	
							</tr>	
							<tr>
								<td width="20%" height="40" class="biantiao_you">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
										<tr>
											<td width="25%" align="right" class="font_1">
												<nobr>编制人数</nobr>
											</td>																				
										</tr>
									</table>																		
								</td>
								<td width="30%" height="40" class="biantiao_you">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
										<tr>																				
											<td width="100%" align="left">
													<form:input path="formation" 
															class="validate[custom[phone]]"
															id="formation"
															style="width:90%" />
											</td>																				
										</tr>
									</table>
								</td>
								<td width="20%" height="40" class="biantiao_you">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
											<tr>
												<td width="25%" align="right" class="font_1">
													<nobr>座机电话</nobr>
												</td>																				
											</tr>
										</table>																		
								</td>
								<td width="30%" height="40">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
										<tr>																				
											<td width="100%" align="left">
												<form:input path="tel" 
															class="validate[custom[phone]]"
															id="tel"
															style="width:90%" />
											</td>																				
										</tr>
									</table>
								</td>
							</tr>																																
							<!-- 行分割线 -->
							<tr>
								<td height="1" class="biaotiao_xia" colspan="4"></td>																	
							</tr>	
							<tr>	
								<td width="20%" height="30" class="biantiao_you">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
											<tr>
												<td width="25%" align="right" class="font_1">
													<nobr>机构地址</nobr>
												</td>																				
											</tr>
										</table>																		
								</td>
								<td width="30%" height="30" colspan="4">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
										<tr>																				
											<td width="100%" align="left" >
												<div style="vertical-align: baseline;">
													<form:hidden path="longitude" id="longitude" /> 
													<form:hidden path="latitude" id="latitude" /> 
													<form:input path="addr" 
															id="addr"
															onclick="positionSelect();"
															readonly="readonly" style="width:96.5%" />
												</div>
											</td>
										</tr>
									</table>
								</td>	
							</tr>
							<!-- 行分割线 -->
							<tr>
								<td colspan="4">
									<div style="height: 1;" class="biaotiao_xia"></div>
								</td>
							</tr>
							<tr>	
								<td width="20%" height="30" class="biantiao_you">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
											<tr>
												<td width="25%" align="right" class="font_1">
													<nobr>机构简介</nobr>
												</td>																				
											</tr>
										</table>																		
								</td>
								<td width="30%" height="30" colspan="4">
									<table width="100%" border="0" cellspacing="5" cellpadding="5">
										<tr>																				
											<td width="100%" align="left">
												<form:textarea path="intro" rows="12" cols="79" style="width:96.5%"/>
											</td>
										</tr>
									</table>
								</td>	
							</tr>
							<!-- 行分割线 -->
							<tr>
								<td colspan="4">
									<div style="height: 1;" class="biaotiao_xia"></div>
								</td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
		</div>
	</form:form>
</body>
</html>
